<template>
  <view class="page-home">
    <!-- <login></login> -->
    <!-- <textarea @input="bindTextAreaBlur" placeholder="请输入面试链接" v-model="detailUrl" auto-height  style="padding:12rpx; min-height: 160rpx; width: 90%; border:#ddd 1px solid;margin-bottom: 24rpx; background:#fff;"/>
    <pk-btn @click="hanlderInter">跳转到面试</pk-btn> -->
    <!-- <view>小程序code码</view>

    <textarea @input="bindTextAreaBlur" placeholder="请输入面试链接" v-model="code" auto-height  style="padding:12rpx; min-height: 160rpx; width: 90%; border:#ddd 1px solid;margin-bottom: 24rpx; background:#fff;"/>

    <textarea v-model="phonenumber" auto-height  style="padding:12rpx; min-height: 160rpx; width: 90%; border:#ddd 1px solid;margin-bottom: 24rpx; background:#fff;"/>

    <button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">获取手机号码</button>
    <pk-btn @click="gotv('/pages/resume/index')">我的简历</pk-btn>

    <pk-btn @click="gotv('/pages/interview/index')">我的面试</pk-btn> -->
    
   <!-- <pk-grid :config="config" col="4" />  

    <pk-btn>2222222</pk-btn>
    <pk-btn type="info">2222444424422</pk-btn>
    <pk-btn type="success" size="mini" plain>2222222</pk-btn>
    <pk-cell :cellconfig="cellconfig" :data="data">
      <template #aaa>bbbb</template>

    </pk-cell>
    <pk-card>
      <template #headerright>22222</template>
      222
    </pk-card> -->


    <!-- <pk-card size="small">
      <template #headerright>22222</template>
      222
    </pk-card> -->


    <!-- <pk-card size="large">
      <template #headerright>22222</template>
      222
    </pk-card> -->

    

    
  </view>
</template>

<script>
import { miniProgramextractOpenid, miniProgramAuthorization } from '@/api/api'
// import login from '@/components/bus/login'
import { setItem } from '@/utils/store'
export default {
  data() {
    return {
      title: 'Hello',
      detailUrl: '',
      code: '',
      phonenumber: '',
      openId: '',
      token: ''
    }
  },
  onLoad() {
    this.getCode()
  },
  onShow() {
    if (this.token) {
      this.gotv('/pages/resume/index') 
    }
  }, 
  methods: { 
    gotv (e) {
      uni.navigateTo({
        url: e
      });
    },
    getCode () {
      let _this = this;
      wx.login({
        success (res) { 
          if (res.code) {
            //发起网络请求
            _this.code = res.code
            _this.getOpenid()
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    },
    getOpenid () {
      miniProgramextractOpenid({
        code: this.code
      }).then(res => {
        console.log('-=----------------------', res)
        this.openId = res.data.openid
        if(res.data.token) {
          this.setSystemInfo(res.data)
        }
      })
    },
    // 解密手机号码 并绑定用户
    bindPhone (e) {
      miniProgramAuthorization({
        openid: this.openId,
        iv: e.iv,
        encryptedData: e.encryptedData
      }).then(res => {
        this.setSystemInfo(res.data)
      })
    },
    setSystemInfo (e) {
      this.token = e.token
      setItem('bytesbayToken', e.token)
      setItem('openId', e.openid)
      setItem('userinfo', JSON.stringify(e.profile))
      this.gotv('/pages/interview/index') 
    },
    onGetPhoneNumber (e) {
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
        this.bindPhone(e.detail)
      }
      
      // console.log(e.detail)
      // this.encryptedData = e.detail.encryptedData
      // console.log(e.detail.code)  // 动态令牌
      // console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
      // console.log(e.detail.errno)  // 错误码（失败时返回）
    },
    gotv (e) {
      uni.navigateTo({
        url: e
      });
    },
    bindTextAreaBlur (e) {
      this.detailUrl = e.detail.value
    },
    hanlderInter () {
      uni.navigateTo({
        url: '/pages/webview/index?weburl=' + encodeURIComponent(this.detailUrl)
      });
    }
  },
}
</script>

<style>
.page-home {
  height: 100vh;
  background: #F5F8FC;
  padding: 24rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
 
</style>
